﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using BLL.Entities;
using System.Web.UI.HtmlControls;

namespace GMS
{
    public partial class Pages_Controls_Shape1 : System.Web.UI.UserControl
    {
        private bool errMsgFlag = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                img1.ImageUrl = SessionVariable.Instance.UserDetails.SelectedShapeEnterInputsUrl;
            }
            if (dsA.IsPostBack)
            {
                ToggleBacksplash(dsA);
            }
            if (dsB.IsPostBack)
            {
                ToggleBacksplash(dsB);
            }
            if (dsC.IsPostBack)
            {
                ToggleBacksplash(dsC);
            }
            if (dsD.IsPostBack)
            {
                ToggleBacksplash(dsD);
            }
            if (dsE.IsPostBack)
            {
                ToggleBacksplash(dsE);
            }
            if (dsF.IsPostBack)
            {
                ToggleBacksplash(dsF);
            }
        }

        private void ToggleBacksplash(Pages_Controls_DimensionSelector dimSelector)
        {
            RadioButtonList rblSeleted = (RadioButtonList)dimSelector.FindControl("rblSideType");
            CheckBox cbBacksplash = (CheckBox)dimSelector.FindControl("cbBacksplash");

            if (rblSeleted.SelectedIndex > 0)
            {
                if (string.Compare(rblSeleted.SelectedItem.Text.ToUpper(), "EDGE") == 0 ||
                    string.Compare(rblSeleted.SelectedItem.Text.ToUpper(), "APPLIANCE") == 0)
                {
                    dimSelector.IsBackSplashEnabled = false;
                    cbBacksplash.Checked = false;
                }
            }
            if (cbBacksplash.Checked == true)
                rblSeleted.SelectedIndex = 0;
        }

        public bool OperationsUC()
        {
            int validationErrorCnt = 0;
            int dimA = 0, dimC = 0, dimB = 0, dimD = 0, dimE = 0, dimF = 0;
            
            SessionVariable.Instance.UserDetails.Cost.Edge = 0;
            
            validationErrorCnt += AddShapeDimension(dsA, Side.Sides.A, out dimA);
            validationErrorCnt += AddShapeDimension(dsB, Side.Sides.B, out dimB);
            validationErrorCnt += AddShapeDimension(dsC, Side.Sides.C, out dimC);
            validationErrorCnt += AddShapeDimension(dsD, Side.Sides.D, out dimD);
            validationErrorCnt += AddShapeDimension(dsE, Side.Sides.E, out dimE);
            validationErrorCnt += AddShapeDimension(dsF, Side.Sides.F, out dimF);

            if (errMsgFlag)
            {
                ShowMessage("Enter dimensions(in inches) and select options (wall/appliance/edge) for all the sides mentioned");
            }

            if (validationErrorCnt == 0)
            {
                SessionVariable.Instance.UserDetails.Cost.Surface = CalculateArea(Utility.CalculateShapeSizeIntoFT(dimA),
                                                                                Utility.CalculateShapeSizeIntoFT(dimB),
                                                                                Utility.CalculateShapeSizeIntoFT(dimC),
                                                                                Utility.CalculateShapeSizeIntoFT(dimD),
                                                                                Utility.CalculateShapeSizeIntoFT(dimE),
                                                                                Utility.CalculateShapeSizeIntoFT(dimF));
                return true;
            }
            return false;
        }

        private double  CalculateArea(double dimA, double dimB, double dimC, double dimD, double dimE, double dimF)
        {
            // refer shape4EnterInputs.jpg for side literals A, ...H
            double Block1 = dimF * dimA;
            double Block2 = dimC * (dimB - dimF);

            if (Block2 < 0)
                Block2 = 0; 

            double result = Block1 + Block2;

            if (result > 0)
                return (result);
            else
                return 0;
        }

        private int AddShapeDimension(Pages_Controls_DimensionSelector dsObj, Side.Sides sideLiteral, out int dim)
        {
            TextBox txt = (TextBox)dsObj.FindControl("txtDimension");
            RadioButtonList rbl = (RadioButtonList)dsObj.FindControl("rblSideType");
            CheckBox cb = (CheckBox)dsObj.FindControl("cbBacksplash");
            dim = 0;
            int res = 0;
            int retValue = 1;

             if (txt.Enabled)
             {
                 if (Int32.TryParse(txt.Text, out res) == false)
                 {
                     errMsgFlag = true;
                 }
             }
             if (rbl.SelectedIndex == -1)
             {
                 errMsgFlag = true;
             }
             
             if (
                 errMsgFlag == false &&
                 rbl != null &&
                 cb != null &&
                 rbl.SelectedIndex > -1)
             {
                 // Store all these sidetypes into Session
                 List<BLL.Entities.ShapeDimension> sdList = new List<BLL.Entities.ShapeDimension>();

                 if (SessionVariable.Instance.UserDetails.Shape != null)
                 {
                     sdList = SessionVariable.Instance.UserDetails.Shape;
                 }

                 // A Side
                 ShapeDimension sD = new ShapeDimension();
                 sD.IsBackSplash = cb.Checked;
                 if (txt.Visible == true && !string.IsNullOrEmpty(txt.Text))
                     int.TryParse(txt.Text, out dim);
                 sD.Measurement = dim;
                 sD.SideTypeId = Convert.ToInt32(rbl.SelectedValue);
                 sD.shapeId = SessionVariable.Instance.UserDetails.ShapeId;
                 sD.Side = sideLiteral;
                 sdList.Add(sD);

                 SessionVariable.Instance.UserDetails.Shape = sdList;
                 // SFT. means x inches/144
                 if (cb.Checked)
                     SessionVariable.Instance.UserDetails.Cost.Backsplash += Convert.ToInt32(
                         GMS.Utility.CalculateShapeSizeIntoFT(Convert.ToInt32(GMS.Utility.CalculateShapeSizeIntoFT(dim * 4))));

                 if (string.Compare(rbl.SelectedItem.Text.ToUpper(), "EDGE") == 0)
                 {
                     SessionVariable.Instance.UserDetails.Cost.Edge += Convert.ToInt32(GMS.Utility.CalculateShapeSizeIntoFT(dim));
                 }
                 retValue = 0;
             }
            return retValue;
        }

        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                Control msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }

        }

    }
}